package com.safe.first.infrastructure.constants;

import com.safe.first.common.constants.BaseEnum;
import com.safe.first.common.constants.CodeEnum;
import com.safe.first.common.exception.BizException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Optional;

/**
 * @author Mickey
 * @since 2022/6/26
 **/
@Getter
@AllArgsConstructor
public enum GrantType implements BaseEnum<GrantType> {
    USERNAME(0, "myUsernamePasswordAuthenticationFilter"),
    VERIFICATION_CODE(1, "verificationCodeAuthenticationFilter"),
    ;

    private final Integer code;
    private final String name;

    public static GrantType of(Integer code) {
        return Optional.ofNullable(BaseEnum.parseByCode(GrantType.class, code)).orElseThrow(() -> new BizException(CodeEnum.FAIL));
    }

    public static GrantType of(String code) {
        return Optional.ofNullable(BaseEnum.parseByCode(GrantType.class, Integer.parseInt(code))).orElseThrow(() -> new BizException("不支持的grantType"));
    }

}
